﻿/*
 * Created by SharpDevelop.
 * User: Alexey
 * Date: 17.07.2013
 * Time: 15:23
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace DrawTest
{
	/// <summary>
	/// Description of SecondForm.
	/// </summary>
	public partial class SecondForm : Form
	{
		private int counterFlashing = 0;
		private List<Image> mainImages = new List<Image>();
		private DataStore dataStore;
		private int timeBrakCount = Consts.delayFlashSecForm;
		//private int angleRotatRec = 0;
		private HashSet<MyPoint> allRecPoint = new HashSet<MyPoint>();
		private List <AirCraft> needAirCrafts = new List<AirCraft>();
		private bool captureCoordinatesSecForm = false;
		public int SizeMainImgSecFormX;
		public int SizeMainImgSecFormY;
		public int IncreaseRectangle = 3;
		
		public SecondForm()
		{
			InitializeComponent();
		}
		
		public void initialize(DataStore dataStore)
		{
			this.dataStore = dataStore;
		}
		
		public void showForm()
		{
			this.ShowDialog();
			this.BringToFront();
		}

		
		public void onTimer()
		{
			if (!this.Visible){
				return;
			}
			label1.Text = dataStore.CurrentAngle.ToString();
			
			
			Invalidate();
		}
		
		void BtnHideClick(object sender, EventArgs e)
		{
			this.Hide();
		}
		
		
		void PaintFlashPic(Graphics dc)
		{
			RectangleF rect = new RectangleF(0,0,SizeMainImgSecFormX,SizeMainImgSecFormY);
			
			dc.TranslateTransform(Consts.CoordinateSystemSecFormX,Consts.CoordinateSystemSecFormY);//set zero coordinates at predefined points (center of circle in image)
			dc.DrawImage(mainImages[counterFlashing], rect);//draw background
			dc.TranslateTransform(-Consts.CoordinateSystemSecFormX,-Consts.CoordinateSystemSecFormY);
			
			if(timeBrakCount <= 0){
				counterFlashing++;  //замедление, уменшением переменной timeBrakCoun
				timeBrakCount = Consts.delayFlashSecForm;
			}
			if(counterFlashing == mainImages.Count){
				counterFlashing = 0; //обнуление счетчика отрисовки
			}
			
			timeBrakCount--;
			
		}
		
		
		void PaintAirCraft(Graphics dc, int angle)
		{
			foreach (AirCraft airCraft in needAirCrafts)
			{
				MyPoint currentAirCraftPosition = airCraft.getObjectPosition();
			
				MyPoint center = new MyPoint(Consts.CoordCenterOfRotationX,Consts.CoordCenterOfRotationY);
				MyAngleLen currentAngLen = FlyingObject.FindAngleAndLength(currentAirCraftPosition, center);
				
				currentAngLen.Ang -= angle;
				//currentAngLen.Len = currentAngLen.Len*IncreaseRectangle;
				currentAngLen.Len = currentAngLen.Len;
				
				dc.TranslateTransform(SizeMainImgSecFormX/2+Consts.CoordinateSystemSecFormX,SizeMainImgSecFormY+Consts.CoordinateSystemSecFormY);
				dc.RotateTransform(currentAngLen.Ang);
				
				dc.FillRectangle(new SolidBrush(Color.Red), 0, 0, 3, 3);
				dc.FillRectangle(new SolidBrush(Color.Red), 0, -currentAngLen.Len, 1, 1);
				
				dc.RotateTransform(-currentAngLen.Ang);
				dc.TranslateTransform(-SizeMainImgSecFormX/2-Consts.CoordinateSystemSecFormX,-SizeMainImgSecFormY-Consts.CoordinateSystemSecFormY);
			}
				
		}
		
		void SecondFormPaint(object sender, PaintEventArgs e)
		{
			Graphics dc = e.Graphics;
			PaintFlashPic(e.Graphics);
			
			FindFlyOject(dataStore.AirCrafts, allRecPoint);
			
			PaintAirCraft(e.Graphics, dataStore.angleRotatRec);
			
		}
		
		
		void SecondFormLoad(object sender, EventArgs e)
		{
			string[] filePaths = Directory.GetFiles(@"resources\SecondFrom\");
			foreach(String oneFile in filePaths){
				mainImages.Add(Image.FromFile(oneFile));
			}
			FindRecPoints(dataStore.angleRotatRec);
		}
		
		
		void FindRecPoints(int angle)
		{
			int cathetHeight = 0;
			
			MyPoint centerRotation = new MyPoint(Consts.CoordCenterOfRotationX, Consts.CoordCenterOfRotationY);
			MyPoint bottomRight = new MyPoint(Consts.CoordCenterOfRotationX + Consts.WidthRecSecForm/2,Consts.CoordCenterOfRotationY);
			MyPoint bottomLeft = new MyPoint(Consts.CoordCenterOfRotationX - Consts.WidthRecSecForm/2,Consts.CoordCenterOfRotationY);
			cathetHeight = (int)Math.Round(Math.Sqrt(Consts.FarLineLenght*Consts.FarLineLenght - Consts.WidthRecSecForm*Consts.WidthRecSecForm/4));
			MyPoint topRight = new MyPoint(bottomRight.X,Consts.FarLineLenght - cathetHeight);
			MyPoint topLeft = new MyPoint(bottomLeft.X,Consts.FarLineLenght - cathetHeight);
			//находим точки прямоугольника расположенного на 0 градусов
			
			SizeMainImgSecFormX = (bottomRight.X - topLeft.X)*IncreaseRectangle;
			SizeMainImgSecFormY = (bottomRight.Y - topLeft.Y)*IncreaseRectangle;
			
			MyAngleLen pointToRotaBL = FlyingObject.FindAngleAndLength(bottomLeft, centerRotation);
			MyAngleLen pointToRotaBR = FlyingObject.FindAngleAndLength(bottomRight, centerRotation);
			MyAngleLen pointToRotaTR = FlyingObject.FindAngleAndLength(topRight, centerRotation);
			MyAngleLen pointToRotaTL = FlyingObject.FindAngleAndLength(topLeft, centerRotation);
			//переводим из декар. сис. в угол-длина
			
			pointToRotaBL.Ang+=angle;
			pointToRotaBR.Ang+=angle;
			pointToRotaTR.Ang+=angle;
			pointToRotaTL.Ang+=angle;
			//добавляем нужный угол
			
			bottomLeft = FlyingObject.FindDecartPoint(centerRotation, pointToRotaBL);
			bottomRight = FlyingObject.FindDecartPoint(centerRotation, pointToRotaBR);
			topRight = FlyingObject.FindDecartPoint(centerRotation, pointToRotaTR);
			topLeft = FlyingObject.FindDecartPoint(centerRotation, pointToRotaTL);
			//переводим обратно в декар. сис.
			
			
			List<MyPoint>  leftRecSide= FlyingObject.findPath(bottomLeft,topLeft);
			List<MyPoint>  rightRecSide= FlyingObject.findPath(bottomRight,topRight);
			List<MyPoint>  clipboard = new List<MyPoint>();
			
			
			
			int minLen = Math.Min(leftRecSide.Count,rightRecSide.Count);
			
			allRecPoint.Clear();
			
			
			for(int i=0; i<minLen; i++){
				clipboard = FlyingObject.findPath(leftRecSide[i],rightRecSide[i]);
				foreach (MyPoint point in clipboard){
					allRecPoint.Add(point);
				}
			}

		}
		
		
		void FindFlyOject(List <AirCraft> airCrafts, HashSet<MyPoint> allPoints)
		{
			needAirCrafts.Clear();
			foreach (AirCraft airCraft in airCrafts)
			{
				MyPoint currentAirCraftPosition = airCraft.getObjectPosition();
				if(allPoints.Contains(currentAirCraftPosition)){
					needAirCrafts.Add(airCraft);
				}
			}
		}
		
		
		
		void SecondFormKeyDown(object sender, KeyEventArgs e)
		{
			if(e.KeyCode == Keys.A){
				captureCoordinatesSecForm = true;
			}
			if(e.KeyCode == Keys.S){
				captureCoordinatesSecForm = false;
			}
			Invalidate();
		}
		
		void SecondFormMouseMove(object sender, MouseEventArgs e)
		{
			if (captureCoordinatesSecForm){
				int coordX = e.X;
				int coordY = e.Y;
				textBox1.Text = coordX.ToString();
				textBox2.Text = coordY.ToString();
			}
		}
		
	}
}
